package com.cgtong.android.activity;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.text.TextUtils;

import com.cgtong.android.R;
import com.cgtong.android.adapter.WonderfulActivityListAdapter;
import com.cgtong.android.base.BaseTitleActivity;
import com.cgtong.android.bean.ActivityBean;
import com.cgtong.android.bean.BannerBean;
import com.cgtong.android.http.ConstantManager;
import com.cgtong.android.mvp.contract.WonderfulActivityContract;
import com.cgtong.android.mvp.presenter.WonderfulActivityPresenter;
import com.cgtong.android.util.PostUtil;
import com.cgtong.android.widget.GlideApp;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.kingja.loadsir.callback.SuccessCallback;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Sky.Bing on 2017/10/19.
 */

public class WonderfulActivitiesActivity extends BaseTitleActivity implements WonderfulActivityContract.View {
    @BindView(R.id.refresh_layout)
    public SwipeRefreshLayout refreshLayout;
    @BindView(R.id.myparticipation_list)
    public RecyclerView mActivitiesRecyclerView;

    private int maxId;
    private String accessToken;
    private WonderfulActivityListAdapter mWonderfulActivityListAdapter;
    private WonderfulActivityPresenter mWonderfulActivityPresenter;

    List<String> mBannerImageUrls;
    List<String> mBannerLinkUrls;

    @Override
    public int getLayoutId() {
        return R.layout.activity_my_participation;
    }

    @Override
    protected String getContentTitle() {
        return getResources().getString(R.string.title_wonderful_activities);
    }

    @Override
    protected void loadDataByNet() {
        mBannerImageUrls = new ArrayList<>();
        mBannerLinkUrls = new ArrayList<>();
        mWonderfulActivityPresenter = new WonderfulActivityPresenter(this);
        mWonderfulActivityListAdapter = new WonderfulActivityListAdapter(this);
        mActivitiesRecyclerView.setAdapter(mWonderfulActivityListAdapter);
        mActivitiesRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mWonderfulActivityListAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        //fix 2017/12/15:server return all data
        /*mWonderfulActivityListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                maxId = mWonderfulActivityListAdapter.getData().get(mWonderfulActivityListAdapter.getData().size() - 1).getId();
                mWonderfulActivityPresenter.loadMoreActivity(maxId);
            }
        }, mActivitiesRecyclerView);*/
        mWonderfulActivityListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                ActivitiesDetailActivity.actionStart(WonderfulActivitiesActivity.this, mWonderfulActivityListAdapter.getData().get(i).getId());
            }
        });
        mWonderfulActivityListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                ActivitiesDetailActivity.actionStart(WonderfulActivitiesActivity.this, mWonderfulActivityListAdapter.getData().get(i).getId());
            }
        });
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                maxId = 0;
                mWonderfulActivityPresenter.getBannerList();
                mWonderfulActivityPresenter.getActivityList(maxId);
            }
        });
        refreshLayout.setColorSchemeResources(R.color.colorAccent);
        addHeadView();
        mWonderfulActivityPresenter.getBannerList();
        mWonderfulActivityPresenter.getActivityList(maxId);
    }

    @Override
    public void setEnableLoadMore(boolean enabled) {
        mWonderfulActivityListAdapter.setEnableLoadMore(enabled);
    }

    @Override
    public void setRefreshing(boolean isRefresh) {
        refreshLayout.setRefreshing(isRefresh);
    }

    @Override
    public void showActivityList(final boolean isRefresh, final List<ActivityBean> activityBeanList) {
        final int size = activityBeanList == null ? 0 : activityBeanList.size();
        if (size > 0) {
            if (isRefresh) {
                mWonderfulActivityListAdapter.setNewData(activityBeanList);
            } else {
                if (size > 0) {
                    mActivitiesRecyclerView.post(new Runnable() {
                        @Override
                        public void run() {
                            mWonderfulActivityListAdapter.addData(activityBeanList);
                        }
                    });
                }
            }
            mActivitiesRecyclerView.post(new Runnable() {
                @Override
                public void run() {
                    if (size < ConstantManager.MAX_WONDERFUL_NEWS_COUNT) {
                        mWonderfulActivityListAdapter.loadMoreEnd(isRefresh);
                    } else {
                        mWonderfulActivityListAdapter.loadMoreComplete();
                    }
                }
            });
        } else {
            mWonderfulActivityListAdapter.setEmptyView(R.layout.layout_empty, (ViewGroup) mActivitiesRecyclerView.getParent());
        }
        PostUtil.postCallbackDelayed(loadService, SuccessCallback.class);
    }

    private Banner headView;

    private void addHeadView() {
        headView = (Banner) LayoutInflater.from(this).inflate(R.layout.adapter_activity_head_view, (ViewGroup) mActivitiesRecyclerView.getParent(), false);
        headView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
//        //设置显示轮播图片区域尺寸比例
//        ViewGroup.LayoutParams bannerLp = (ViewGroup.LayoutParams) headView.getLayoutParams();
//        float width = ScreenUtils.getScreenWidth();
//        bannerLp.width = (int)width;
//        bannerLp.height = (int)(width / 2.5);
//        headView.setLayoutParams(bannerLp);
        mWonderfulActivityListAdapter.addHeaderView(headView);
        //initBanner(headView);
    }

    private void initBanner(Banner banner) {
        banner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                imageView.setImageResource(R.drawable.banner);  //TODO:SKY.BING
            }
        });
        List<String> urls = new ArrayList<>();
        urls.add("temp1");
        urls.add("temp2");
        banner.setImages(urls);
        banner.start();
    }

    @Override
    public void onLoadError() {
        setRefreshing(false);
        PostUtil.postCallbackDelayed(loadService, SuccessCallback.class);
        mWonderfulActivityListAdapter.setEmptyView(R.layout.layout_error, (ViewGroup) mActivitiesRecyclerView.getParent());
    }

    @Override
    public void onLoadMoreError() {
        setRefreshing(false);
        setEnableLoadMore(false);
        mWonderfulActivityListAdapter.loadMoreComplete();
    }

    @Override
    protected void onDataReloadByNet(View v) {
    }

    @Override
    public void showBanner(List<BannerBean> bannerBeanList) {
        headView.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                GlideApp.with(context).load(path).placeholder(R.drawable.icon_default_banner_loading).error(R.drawable.icon_default_banner_error).into(imageView);
            }
        });
        if (null != mBannerImageUrls)
            mBannerImageUrls.clear();
        if (null != mBannerLinkUrls)
            mBannerLinkUrls.clear();
        for (int i = 0; i < bannerBeanList.size(); i++) {
            if (i < ConstantManager.HOME_MAX_BANNER_LIST) {
                BannerBean bannerBean = bannerBeanList.get(i);
                mBannerImageUrls.add(bannerBean.getImageUrl());
                mBannerLinkUrls.add(bannerBean.getLink());
            }
        }
        headView.setImages(mBannerImageUrls);
        headView.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                String linkUrl = mBannerLinkUrls.get(position);
                if(!TextUtils.isEmpty(linkUrl)){
                    WebViewActivity.actionStart(WonderfulActivitiesActivity.this, linkUrl);
                }
            }
        });
        headView.start();
    }

    @Override
    public void onStop() {
        super.onStop();
        if (null != headView) {
            headView.stopAutoPlay();
        }
    }
}